package com.iteaj.network.test.message.line;

import com.alibaba.fastjson.JSONObject;
import com.iteaj.network.ProtocolType;
import com.iteaj.network.test.message.TMessageHead;
import com.iteaj.network.utils.ByteUtil;

public class LineMessageHead extends TMessageHead {

    protected LineMessageHead() {
        super(null, null, null);
    }

    protected LineMessageHead(byte[] message) {
        super(message);
    }

    protected LineMessageHead(String equipCode, String messageId, ProtocolType type) {
        super(equipCode, messageId, type);
    }

    public static LineMessageHead build(byte[] message) {
        int length = ByteUtil.bytesToInt(message);
        byte[] headMsg = ByteUtil.subBytes(message, 4, length + 4);
        return JSONObject.parseObject(headMsg, LineMessageHead.class);
    }

    public static LineMessageHead build(String equipCode, String messageId, ProtocolType type) {
        LineMessageHead lineMessageHead = new LineMessageHead(equipCode, messageId, type);
        byte[] bytes = JSONObject.toJSONBytes(lineMessageHead);
        byte[] message = new byte[bytes.length + 4];
        ByteUtil.addBytes(message, bytes, 4);
        ByteUtil.addBytes(message, ByteUtil.intToBytes(bytes.length), 0);

        lineMessageHead.setMessage(message);
        return lineMessageHead;
    }
}
